package com.xiaoq.users.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Userinfos;
import com.taobao.api.request.OpenimUsersAddRequest;
import com.taobao.api.request.OpenimUsersDeleteRequest;
import com.taobao.api.request.OpenimUsersGetRequest;
import com.taobao.api.request.OpenimUsersUpdateRequest;
import com.taobao.api.response.OpenimUsersAddResponse;
import com.taobao.api.response.OpenimUsersDeleteResponse;
import com.taobao.api.response.OpenimUsersGetResponse;
import com.taobao.api.response.OpenimUsersUpdateResponse;
import com.xiaoq.coms.util.JsonUtil;

/**
 * 
 * @author wang zhihao created 2018/4/12
 *
 */
@Component
public class IMUserService {
	private static final String AppKey = "24850619";
	private static final String AppSecret = "39abb8292fde8c23cb9e4b65016257e0";
	
	private static final Logger LOG = LoggerFactory.getLogger(IMUserService.class);
	
	private static final String BAICHUAN_URL = "http://gw.api.taobao.com/router/rest";
	
	public static void main(String[] args) {
		IMUserInfoVo imUserInfoVo = new IMUserInfoVo();
		imUserInfoVo = new IMUserInfoVo();
		imUserInfoVo.setNick("欣然");
		imUserInfoVo.setIconUrl("http://p0sd7vv3m.bkt.clouddn.com/MTUyMTA4NzgzMjI5NV8xNTIxMDg3ODMxOTcwNzQxLmpwZzE1MjEwODc4MzM0MzM=");
		imUserInfoVo.setUserId("N971621660710932480");
		imUserInfoVo.setPassword("6b96c7c1abfb271c620c65670302289f");
		imUserInfoVo.setName("欣然");
		String result = new IMUserService().addImUsers(imUserInfoVo);
//		String result = new IMUserService().addImUsers(imUserInfoVo);
//		IMResultVo imResultVo = JsonUtil.asObject(result, IMResultVo.class);
//		List<String> failMsg = imResultVo.getOpenimUsersUpdateResponse().getFailMsg().getString();
//		System.out.println(failMsg);
//		new IMUserService().getImUsers("N986074217566965760");
	}
	
	/**
	 * 向阿里百川注册一个用户
	 * @param imUserInfoVo
	 */
	@Async
	public  String addImUsers(IMUserInfoVo imUserInfoVo){
		TaobaoClient client = new DefaultTaobaoClient(BAICHUAN_URL, AppKey, AppSecret);
		OpenimUsersAddRequest req = new OpenimUsersAddRequest();
		List<Userinfos> userInfosList = new ArrayList<Userinfos>();
		Userinfos userInfos = new Userinfos();
		userInfosList.add(userInfos);
		userInfos.setNick(imUserInfoVo.getNick());
		userInfos.setIconUrl(imUserInfoVo.getIconUrl());
		userInfos.setUserid(imUserInfoVo.getUserId());
		userInfos.setPassword(imUserInfoVo.getPassword());
		userInfos.setName(imUserInfoVo.getName());
		req.setUserinfos(userInfosList);
		OpenimUsersAddResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			LOG.error(e.getErrMsg(),e);
		}
		String result = rsp.getBody();
		LOG.info("批量添加百川用户结果"+result);
		return result;
		
	}
	
	/**
	 * 批量获取阿里百川上的用户
	 * @param userIds 如果多个userIds中间使用","隔开
	 */
	@Async
	public  void getImUsers(String userIds){
		TaobaoClient client = new DefaultTaobaoClient(BAICHUAN_URL, AppKey, AppSecret);
		OpenimUsersGetRequest req = new OpenimUsersGetRequest();
		req.setUserids(userIds);
		OpenimUsersGetResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			LOG.error(e.getErrMsg(),e);
		}
		String result = rsp.getBody();
		LOG.info("批量获取百川用户结果为:"+result);
		
	}
	
	/**
	 * 批量删除阿里百川上的用户
	 * @param userIds userIds 如果多个userIds中间使用","隔开
	 */
	@Async
	public  void deleteImUsers(String userIds){
		TaobaoClient client = new DefaultTaobaoClient(BAICHUAN_URL, AppKey, AppSecret);
		OpenimUsersDeleteRequest req = new OpenimUsersDeleteRequest();
		req.setUserids(userIds);
		OpenimUsersDeleteResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			LOG.error(e.getErrMsg(),e);
		}
		String result = rsp.getBody();
		LOG.info("百川批量删除用户结果为"+result);
	}
	
	/**
	 * 修改阿里百川的一个用户
	 * @param imUserInfoVo
	 */
	@Async
	public  String updateImUsers(IMUserInfoVo imUserInfoVo){
		TaobaoClient client = new DefaultTaobaoClient(BAICHUAN_URL, AppKey, AppSecret);
		OpenimUsersUpdateRequest req = new OpenimUsersUpdateRequest();
		List<Userinfos> userInfosList = new ArrayList<Userinfos>();
		Userinfos userInfos = new Userinfos();
		userInfosList.add(userInfos);
		userInfos.setNick(imUserInfoVo.getNick());
		userInfos.setIconUrl(imUserInfoVo.getIconUrl());
		userInfos.setUserid(imUserInfoVo.getUserId());
		userInfos.setPassword(imUserInfoVo.getPassword());
		userInfos.setName(imUserInfoVo.getName());
		req.setUserinfos(userInfosList);
		OpenimUsersUpdateResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			LOG.error(e.getErrMsg(),e);
		}
		String result = rsp.getBody();
		LOG.info("修改阿里百川用户结构："+result);
		return result;
	}
	

}
